#ifndef RESULT_H
#define RESULT_H

#include <QObject>
#include <QList>
#include <QDateTime>

#include "types.h"

class Point : public QObject
{
    Q_OBJECT

    Q_PROPERTY(float freq READ getFreq NOTIFY pointChanged)
    Q_PROPERTY(float voltage READ getVoltage NOTIFY pointChanged)
    Q_PROPERTY(double real READ getReal NOTIFY pointChanged)
    Q_PROPERTY(double imag READ getImag NOTIFY pointChanged)

public:
    explicit Point(QObject *parent, double freq = 0.0, double voltage = 0.0, double imag = 0.0, double real = 0.0) : QObject(parent),
        freq(freq), voltage(voltage), real(real), imag(imag) {}

    float getFreq() { return freq; }
    float getVoltage() { return voltage; }
    double getReal() { return real; }
    double getImag() { return imag; }

private:
    float freq;
    float voltage;
    double real;
    double imag;

signals:
    void pointChanged();

public slots:
    void setPoint(float freq, float voltage, double imag, double real)
    {
        this->freq = freq;
        this->voltage = voltage;
        this->real = real;
        this->imag = imag;
        emit pointChanged();
    }
};

class Result : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QList<Point *> points READ getPoints NOTIFY resultChanged)
    Q_PROPERTY(int size READ size NOTIFY resultChanged)
    Q_PROPERTY(QDateTime create_time READ getCreateTime NOTIFY resultChanged)
    Q_PROPERTY(Types::DrivingCurrent driving_current READ getDrivingCurrent CONSTANT)
    Q_PROPERTY(Types::TaskType task_type READ getTaskType CONSTANT)

public:
    explicit Result(Types::TaskType task_type, Types::DrivingCurrent driving_current, QDateTime create_time, QObject *parent = nullptr) : QObject(parent), task_type(task_type), create_time(create_time), driving_current(driving_current) {}
    ~Result()
    {
        qDeleteAll(points);
        points.clear();
    }
    void setTaskType(Types::TaskType type) { task_type = type; }
    Types::TaskType getTaskType() const { return task_type; }
    QList<Point *> getPoints() const { return points; }
    QList<Point *> &getPoints()  { return points; }
    int size() { return points.size(); }
    QDateTime getCreateTime() const { return create_time; }
    Types::DrivingCurrent getDrivingCurrent() const { return driving_current; }

signals:
    void resultChanged();

private:
    QList<Point *> points;

    QDateTime create_time;
    Types::DrivingCurrent driving_current;
    Types::TaskType task_type;

public:
    void addPoint(float freq, float voltage, double imag, double real)
    {
        Point *point = new Point(this, freq, voltage, imag, real);
        points.append(point);
        emit resultChanged();
    }
};
#endif // RESULT_H
